package br.edu.univas.si.engs.view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import br.edu.univas.si.engs.controller.Controller;
import br.edu.univas.si.engs.model.ClientTO;







public class PanelConsultarClienteReserva extends JPanel {
	
	

		
		
		private static final long serialVersionUID = 1L;

		
		private JLabel nomeLabel;
		private JTextField nomeFiled;
		
		public void setNomeFiled(String nomeFiledd) {
			System.out.println(nomeFiledd);
			this.nomeFiled.setText(nomeFiledd) ;
		}




		private GridBagConstraints nomeLabelConstraints;
		private GridBagConstraints nomeFildConstraints;
		
	    private JTable table;
		private DefaultTableModel tableModel;
		private JScrollPane scroll;
		private GridBagConstraints tablecBagConstraints;

		
		private JButton consultarButton;
		private JButton limparButton;
		private ButtonListenerConsultarClienteReserv buttonListenerConsultaclienteReseva;
		
		
		
		private GridBagConstraints consultaBagConstraints;
		private GridBagConstraints limparBagConstraints;
		
		private Controller  controller;
		
		public PanelConsultarClienteReserva(){
			
			setLayout(new GridBagLayout());
			setBorder(new EmptyBorder(6, 6, 6, 6));
			initialize();
			
		}
		
		
		
		public Controller getController() {
			if(controller==null){
			
				controller=new Controller(null, null, null, null, null);
			}
			return controller;
		}




		ArrayList<ButtonListenerConsultarClienteReserv > listenerRes = new ArrayList<ButtonListenerConsultarClienteReserv>();
		
		
		
		
		

		public GridBagConstraints getConsultaBagConstraints() {
			consultaBagConstraints=createConstraintPrototype();
			consultaBagConstraints.gridx=2;
			consultaBagConstraints.gridy=0;
			return consultaBagConstraints;
		}

		public GridBagConstraints getLimparBagConstraints() {
			limparBagConstraints=createConstraintPrototype();
			limparBagConstraints.gridx=3;
			limparBagConstraints.gridy=0;
			return limparBagConstraints;
		}
		
		
		
		public JButton getConsultarButton() {
			if(consultarButton==null){
				consultarButton=new JButton();
				consultarButton.setText("Consultar");
				consultarButton.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
	for (ButtonListenerConsultarClienteReserv	 list : listenerRes) {
							
							if(list!=null){
								list.consultarAction();
							}
						}
						
					}
				});
			}
			return consultarButton;
		}
		/**
		 * @return the limparButton
		 */
		public JButton getLimparButton() {
			
			if(limparButton==null){
				limparButton=new JButton();
				limparButton.setText("Limpar");
				limparButton.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						
						for (ButtonListenerConsultarClienteReserv	 list : listenerRes) {
							
							if(list!=null){
								list.limparAcition();
							}
						}
							
						
						
					}
				});
			
			
			}
			return limparButton;
		}
		


		private void initialize() {
			
			add(getNomeLabel(),getNomeLabelConstraints());
			add(getNomeFiled(),getNomeFildConstraints());
			add(getConsultarButton(),getConsultaBagConstraints());
			add(getLimparButton(),getLimparBagConstraints());
			add(getScroll(),getTablecBagConstraints());	
		}
		

		
		public JLabel getNomeLabel() {
			if(nomeLabel==null){
				nomeLabel= new JLabel();
				nomeLabel.setText("Nome");
			}
			return nomeLabel;
		}
		/**
		 * @param  
		 * @return the nomeFiled
		 */
		public JTextField getNomeFiled() {
			
			if(nomeFiled==null){
				nomeFiled= new JTextField();
				
				
				
			
				
				
			}
			return nomeFiled;
		}
		/**
		 * @return the nomeLabelConstraints
		 */
		
		private GridBagConstraints createConstraintPrototype() {
			GridBagConstraints gbc = new GridBagConstraints();
			int space = 2;
			gbc.fill = GridBagConstraints.HORIZONTAL;
			gbc.insets = new Insets(space, space, space, space);
			return gbc;
		}
		
		
		public GridBagConstraints getNomeLabelConstraints() {
			if(nomeLabelConstraints==null){
				nomeLabelConstraints=createConstraintPrototype();
				nomeLabelConstraints.gridx=0;
				nomeLabelConstraints.gridy=0;
				
			}
			
			return nomeLabelConstraints;
		}
		/**
		 * @return the nomeFildConstraints
		 */
		public GridBagConstraints getNomeFildConstraints() {
			if(nomeFildConstraints==null){
				nomeFildConstraints=createConstraintPrototype();
				nomeFildConstraints.gridx=1;
				nomeFildConstraints.gridy=0;
				nomeFildConstraints.ipadx=550;
				
			}
			return nomeFildConstraints;
		}
		
		
		

		//Borda
		
		private DefaultTableModel getTableModel() {
			if(tableModel == null){
				tableModel = new DefaultTableModel();
				//tableModel.fireTableStructureChanged();
				tableModel.setColumnIdentifiers(new String[]{
					"Codigo","Nome","CPF","RG","Telefone","Celular","Estado","Endereco","Cidade","Status"
				});
			}
			
			
			
			
			
			return tableModel;
		}
		
		

		
		
		private JTable getTable() {
			
				if(table==null){
					table= new JTable();
					
					
					 
					table.setModel(getTableModel());
					table.addMouseListener (new MouseAdapter (){ 
						public void mouseReleased (MouseEvent e) 
						{ 
							if (e.getClickCount () == 2) 
							{ 
								
									int idCliente= Integer.parseInt( (String) table.getValueAt(table.rowAtPoint (e.getPoint ()),0));
									String nomeCliente=(String) table.getValueAt(table.rowAtPoint (e.getPoint ()),1);
									JOptionPane.showMessageDialog(null	, "Cliente escolhido");
									
										
									
									
									FrameConsultarCarroReserva framereserva=new FrameConsultarCarroReserva(null, nomeCliente, idCliente, null, null, true);
									framereserva.setVisible(true);
																    
										
									  
				                         
									}
									
									
							
						}
						 
					}); 
					

					
				}
			
			return table;
		
		}
		
		private JScrollPane getScroll() {
			if(scroll == null){
				scroll = new JScrollPane(getTable());
				scroll.setPreferredSize(new Dimension(0, 0));
				
			
				
			}
			return scroll;
		}
		
		public GridBagConstraints getTablecBagConstraints() {
			tablecBagConstraints = createConstraintPrototype();
			tablecBagConstraints.gridx = 0;
			tablecBagConstraints.gridy = 1;
			tablecBagConstraints.gridwidth = 4;
			tablecBagConstraints.ipady=400;
			return tablecBagConstraints;
		}
		
		
		
		
		
		public void addContact(ArrayList<ClientTO> novoArray ) {
			
		for (ClientTO clientesTo : novoArray) {
			 
			String[] line= new String[]{
				
					Integer.toString(clientesTo.getId()),
					clientesTo.getNome(),
					clientesTo.getCpf(),
					clientesTo.getRg(),
					clientesTo.getTelefone(),
					clientesTo.getCelular(),
					
					};
			getTableModel().addRow(line);
			
		}
			
	}
		
		

		
		
		
		
		
		public void limparTable(){
			tableModel.setNumRows(0);  
		}


		public ClientTO getcontactClienteTo(){
			ClientTO c = new ClientTO();
			c.setNome(getNomeFiled().getText());
			
			return c;
		
		}


		public void getNomeFiled1(String text) {
			nomeFiled.setText(text);
			// TODO Auto-generated method stub
			
		}
		



		public void setButtonListenerConsultaclienteReserva(
				ButtonListenerConsultarClienteReserv buttonListenerConsultaclienteReseva) {
			this.listenerRes.add( buttonListenerConsultaclienteReseva);
			
		}
		
		
		
		
		

	

	
	

}
